<template>
  <div class="full-box shadow-box padding-box">
    <OperateCommon :showSearch="true" :showAdd="false" @search="initData">
      <template #condition>
        <div class="condition-item">
          <el-input
            v-model="queryParams.object_name"
            placeholder="按项目名称关键字查询"
            clearable
          ></el-input>
        </div>
        <div class="condition-item">
          <el-date-picker
            style="width: 150px"
            v-model="queryParams.start_date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="开始时间"
            clearable
          >
          </el-date-picker>
        </div>
        <div class="condition-item">
          <el-date-picker
            style="width: 150px"
            v-model="queryParams.end_date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="结束时间"
            clearable
          >
          </el-date-picker>
        </div>
      </template>
    </OperateCommon>
    <div class="table-wrapper">
      <BaseTable
        :columns="columns"
        :showSort="true"
        :tableData="tableData"
        :pages="{
          show: true,
          count: count,
        }"
        :operate="{
          show: true,
          width: 200,
        }"
        @pagination="pageChange"
      >
        <template #operate="{ current }">
          <el-button
            v-if="current.row.assign_status == 0"
            type="danger"
            size="mini"
            @click="rejectFeedback(current.row)"
            >退回</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="linkTo(`/redAlert/details/${current.row.id}/1`)"
            >详情</el-button
          >
          <!-- <el-button
            type="danger"
            size="mini"
            @click="deleteConfirm(current.row.id)"
            >删除</el-button
          > -->
        </template>
      </BaseTable>
    </div>
    <el-dialog
      title="交办退回"
      :visible.sync="dialog.reject"
      width="700px"
      @close="dialog.reject = false"
    >
      <el-form
        :model="rejectForm"
        ref="rejectForm"
        :rules="rejectRules"
        label-width="80px"
        :inline="false"
      >
        <el-form-item label="退回原因" prop="feedback">
          <el-input
            type="textarea"
            v-model="rejectForm.feedback"
            placeholder=""
            :autosize="{
              minRows: 3,
              maxRows: 6,
            }"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button @click="dialog.reject = false">取消</el-button>
          <el-button type="danger" @click="rejectProblem">退回</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import BaseTable from "@/components/Common/BaseTable.vue";
import OperateCommon from "@/components/Common/OperateCommon.vue";
import LinkTo from "@/mixins/LinkTo";
import {
  getRedAlerAssignList,
  getRedAlertList,
  postRedAlertFeedback,
} from "@/api/RedAlert/Index";
import { required } from "@/utils/eValidate";
import { getCurrentDateTime } from "@/utils/date";
import commonList from "@/mixins/commonList";
export default {
  mixins: [LinkTo, commonList],
  components: { BaseTable, OperateCommon },
  data() {
    return {
      columns: columns,
      tableData: [],
      count: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        object_name: "",
        start_date: "",
        end_date: "",
        dept_id: "",
        assign_status: "",
      },
      dialog: {
        reject: false,
      },
      rejectForm: {
        id: "", // 交办 ID
        status: 2, // 1 接收 2 退回
        status_text: "已退回", // 已反馈 已退回
        feedback: "", // 接收意见
        feedback_date: getCurrentDateTime(), // 接收时间
      },
      rejectRules: {
        feedback: [required],
      },
    };
  },
  methods: {
    initData() {
      if (!this.queryParams.start_date) {
        this.queryParams.start_date = "";
      }
      if (!this.queryParams.end_date) {
        this.queryParams.end_date = "";
      }
      getRedAlerAssignList(this.queryParams).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data;
          // .map((i) => {
          //   switch (parseInt(i.red_solve_status)) {
          //     case 0:
          //       i.red_solve_status = "未交办";
          //       break;
          //     case 10:
          //       i.red_solve_status = "已交办";
          //       break;
          //     case 15:
          //       i.red_solve_status = "交办退回";
          //       break;
          //     case 20:
          //       i.red_solve_status = "待处置";
          //       break;
          //     case 100:
          //       i.red_solve_status = "已处置";
          //       break;
          //   }

          //   return i;
          // });
          this.count = res.num;
        }
      });
    },
    pageChange(val) {
      this.queryParams.pageNum = val.pageNum;
      this.queryParams.pageSize = val.pageSize;
      this.initData();
    },
    deleteConfirm(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteItem(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteItem(id) {
      deleteDept({ id: id }).then((res) => {
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.initData();
        }
      });
    },
    rejectFeedback(item) {
      this.rejectForm.id = item.assign_id;
      this.dialog.reject = true;
    },
    rejectProblem() {
      this.$refs.rejectForm.validate((valid) => {
        if (valid) {
          postRedAlertFeedback(this.rejectForm).then((res) => {
            if (res.code == 0) {
              this.$message.success("退回成功");
              this.initData();
              this.dialog.reject = false;
            }
          });
        }
      });
    },
  },
  created() {
    this.initData();
  },
};

const columns = [
  {
    prop: "object_name",
    label: "项目名称",
    width: "",
    minWidth: "300",
    align: "",
  },
  {
    prop: "dept_name",
    label: "预警单位",
    width: "",
    minWidth: "200",
    align: "",
  },
  {
    prop: "description",
    label: "预警监控点",
    width: "",
    minWidth: "300",
    align: "",
  },
  {
    prop: "trigger_time",
    label: "预警发生时间",
    width: "200",
    minWidth: "",
    align: "",
  },
  {
    prop: "assign_status_text",
    label: "处置状态",
    width: "100",
    minWidth: "",
    align: "",
  },
  {
    prop: "assign_time",
    label: "交办日期",
    width: "200",
    minWidth: "",
    align: "",
  },
];
</script>

<style lang="scss" scoped>
</style>